var statusLists = [
    {
        id: '0',
        text: '会员商品'
    },{
        id: '1',
        text: '会员信息'
    }, {
        id: '2',
        text: '会员卡'
    }];
var ns, toast;
var app = new Vue({
    el: '#app',
    data: {
        orderlist: [],
        statusList: this.statusLists,
        getdata_timeout: null,
        statuscount:[],
        productList:[],
        huiyuanlist:[],//会员列表
        cardmsg:{},
        orderCount: 0,
        status: '0',
        total:0,
        cardStatus:1,//会员卡状态：1-已发放，2-已暂停
        show_pro:false,//是否展开调价
        tiaojiamsg:{},//调价商品信息
        warmingmsg:false,//调价错误提示
        // selectId:0,
        // 会员商品相关
        product:{
            actions: [
                { text: '调价', key: 'change_price' },
                { text: '移除', key: 'del' },
                { text: '上架', key: 'up' },
                { text: '下架', key: 'down' }
            ],
            list:[],
            
        },
        pageindex: 0,
        pagesize: 10,
        is_searching:false,
        can_scroll:false,
        current_item_length: 0,
    },
    methods: {
        // 获取会员商品列表
        get_data:function(){
            var that = this
            that.is_searching = true;
            that.can_scroll = false;
            const params = {
                action:'getbusinessmemberproductlist',
                startRowIndex:that.pageindex,
                pageSize:that.pagesize,
            }
            ns.post(params,function(successed,data,err){
                console.log(JSON.stringify(data))
                that.is_searching = false;
                if (successed) {

                    if (data.data.length == that.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.pageindex == 0) {
                        that.product.list = data.data;
                    } else {
                        that.product.list = that.product.list.concat(data.data);
                    }
                    that.current_item_length = that.product.list.length;
                    that.total=data.total;


                    // const list = data.data||[]
                    console.log(JSON.stringify(that.product.list))
                    that.product.list = that.product.list.map(res=>{
                        res.Selected = false
                        return res
                    })
                } else {
                    that.product.list = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        // 获取会员列表
        huiyuanList:function(){
            var that = this
            that.huiyuanlist=[];
            const params = {
                action:'getmemberlist',
                startRowIndex:that.product.startRowIndex,
                pageSize:that.product.pageSize,
            }
            ns.post(params,function(successed,data,err){
                console.log(JSON.stringify(data))
                if (successed) {
                    if(data.status){
                        that.huiyuanlist=data.data;
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        // 会员卡信息
        cardMsg:function(){
            var that = this
            const params = {
                action:'getbusinessmembercard',
            }
            ns.post(params,function(successed,data,err){
                // console.log(JSON.stringify(data.data))
                if (successed) {
                    if(data.status){
                        if(data.data==null){
                            that.cardmsg=null;
                        }else{
                            that.cardmsg=data.data;
                        }
                        console.log(JSON.stringify(that.cardmsg));
                        
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        // 打开商品更多操作
        handOpenMoreAction(e){
            console.log(JSON.stringify(e));
            const {ID} = e
            this.product.current = e
            // this.product.actions = [
            //     { text: '调价', key: 'change_price' ,disabled},
            //     { text: '调价2', key: '222'},
            // ]
            this.product.list = this.product.list.map(res=>{
                res.Selected = Number(res.ID) === Number(ID)
                return res
            })
        },
        // 点击会员商品更多操作
        handClickMemberProductMoreAction(e){
            console.log(JSON.stringify(e));
            const {key} = e
            if(key === 'change_price'){
                return this.handChangePrice()
            }
            if(key === 'del'){
                return this.handDel()
            }
            if(key === 'up'){
                return this.handUp()
            }
            if(key === 'down'){
                return this.handDown()
            }
        },
        // 改价
        handChangePrice:function(){
            var that = this;
            that.tiaojiamsg.CardID=that.product.current.CardID;
            that.tiaojiamsg.ProductName=that.product.current.ProductName;
            that.tiaojiamsg.SalePrice=that.product.current.SalePrice;
            that.tiaojiamsg.MemberPrice=that.product.current.MemberPrice;
            that.show_pro=true;
        },
        closebox:function(){
            var that = this;
            that.show_pro=false;
        },
        save_editpro:function(){
            var that = this;
            if(that.tiaojiamsg.MemberPrice>that.tiaojiamsg.SalePrice){
                api.toast({
                    msg: '会员价必须小于当前销售价',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            var list=[];
            list.push({ID:that.tiaojiamsg.CardID,MemberPrice:that.tiaojiamsg.MemberPrice});
            const params = {
                action:'savememberproductprice',
                Products:list,
            }
            ns.post(params,function(successed,data,err){
                console.log(JSON.stringify(data))
                if (successed) {
                    if(data.status){
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.show_pro=false;
                        that.get_data();
                        
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })

        },
        // 移除
        handDel:function(){
            var that = this;
            var IDs=[];
            IDs.push(that.product.current.CardID);
            const params = {
                action:'memberproductstatusedit',
                IDs:IDs,
                Type:1,
            }
            ns.post(params,function(successed,data,err){
                if (successed) {
                    console.log(JSON.stringify(data.status));
                    if(data.status){
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        // 上架
        handUp:function(){
            var that = this;
            var IDs=[];
            IDs.push(that.product.current.CardID);
            const params = {
                action:'memberproductstatusedit',
                IDs:IDs,
                Type:2,
            }
            ns.post(params,function(successed,data,err){
                if (successed) {
                    if(data.status){
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })

        },
        // 下架
        handDown:function(){
            var that = this;
            var IDs=[];
            IDs.push(that.product.current.CardID);
            const params = {
                action:'memberproductstatusedit',
                IDs:IDs,
                Type:3,
            }
            ns.post(params,function(successed,data,err){
                if (successed) {
                    if(data.status){
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        onSelect(action) {
            Toast(action.text);
          },
        //配置会员卡
        do_peizhi:function(){
            var that = this;
            ns.openWin('vip_cardedit', '配置会员卡');
        },
        //编辑会员卡
        do_editCard:function(){
            var that = this;
            ns.openWin('vip_cardedit', '编辑会员卡',{
                item:that.cardmsg
            });
        },

        //搜索
        do_search:function(){
            var that = this;
            ns.openWin('search_frm', '搜索');
        },

        //导航栏切换
        randomSwitchBtn: function(name, id, index) {
            var that = this;
            //调用接口给数据赋值
            that.status = id;
            that.orderCount = "";
            that.total.ProcessCount = "";
            // that.get_List();
        },
        // 商品详情
        do_opendetail: function(id) {
            var that = this;
            ns.openWin('product_detail', '商品详情', {
                id: id,
            });
        },
        //添加商品
        do_addProduct:function(){
            var that = this;
            ns.openWin('choose_product', '添加商品',{
                ProductType:'member',
            });
        },
        //暂停发放会员卡
        do_stopSend:function(){
            var that=this;
            that.$dialog.confirm({
                title: '提示信息',
                message: '暂停发放后，新用户无法注册成为会员。已领取用户不受影响。确定要暂停发放吗?',
                confirmButtonText:'确定',
                cancelButtonText:'取消',
            }).then(() => {
                var options = {};
                    options.action = 'businessmembercardstatuschenge';
                    options.Status = 1;
                    ns.post(options, function(succeed, data, err) {
                        console.log(JSON.stringify(data));
                        if (succeed) {
                            if(data.status){
                                api.toast({
                                    msg: "已暂停",
                                    duration: 1000,
                                    location: 'bottom'
                                });
                                that.cardMsg();
                            }
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }, {
                        toast: false
                    });
            }).catch(() => {
                // on cancel
              });
        },
        //继续发送
        do_sendCard:function(){
            var that=this;
            var options = {};
            options.action = 'businessmembercardstatuschenge';
            options.Status = 2;
            ns.post(options, function(succeed, data, err) {
                console.log(JSON.stringify(data));
                if (succeed) {
                    if(data.status){
                        api.toast({
                            msg: "成功发送",
                            duration: 1000,
                            location: 'bottom'
                        });
                        that.cardMsg();
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: false
            });
        },
        //注销会员
        removeVip:function(id){ 
            var that=this;
            var options = {};
            options.action = 'deletemember';
            options.ID = id;
            ns.post(options, function(succeed, data, err) {
                console.log(JSON.stringify(data));
                if (succeed) {
                    if(data.status){
                        api.toast({
                            msg: "成功发送",
                            duration: 1000,
                            location: 'bottom'
                        });
                        that.cardMsg();
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: false
            });

        },

    },
});

apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    ns.init_pull();
    api.addEventListener({
        name: 'do_open_search'
    }, function(ret, err) {
        ns.openWin('search_frm', '搜索');
    });
    api.addEventListener({
        name: 'do_open_fenlei'
    }, function(ret, err) {
        ns.openWin('product_sort', '商品分类');
    });
    api.addEventListener({
        name:'load_huiyuan'
    }, function(ret){
        app.cardMsg();
    });
    api.addEventListener({
        name:'load_productvip'
    }, function(ret){
        app.get_data();
    });
    // api.addEventListener({
    //     name: 'do_add_product'
    // }, function (ret, err) {
    //     console.log(JSON.stringify(ret.value.list));
    //     app.product.list = ret.value.list;
    // });
    // 拉取会员商品列表
    app.get_data();
    app.huiyuanList();
    app.cardMsg();
}
